package com.android.myipc;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by Android Studio.
 * User: pengym
 * Date: 2020/11/10
 * Time: 14:25
 * Name: 作用类：服务端，跨进程通信！
 */
public class MessagerSeriver extends Service {
    private static final String TAG = "messagerService";

    private static class MessagerHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {

                case MyConstants.CHAPTER_2_PATH:
                    Log.i(TAG, "receive msg from Client:" + msg.getData().getString("msg"));
                    Messenger client = msg.replyTo;
                    Message relpyMessage = Message.obtain(null,MyConstants.CHAPTER_FILE_PATH);
                    Bundle bundle = new Bundle();
                    bundle.putString("reply","嗯，你的消息我意见收到了，稍后会回复你。");
                    relpyMessage.setData(bundle);
                    try{
                        client.send(relpyMessage);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    }
private final Messenger mMessenger = new Messenger( new MessagerHandler());

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG,"digq1");
        return mMessenger.getBinder();
    }
}
